package dev.ece.util.tools;

import dev.ece.util.GLUtils;
import dev.ece.util.lib.Matrix4f;

public class Projection {

	private float left;
	
	private float right;
	
	private float bottom;
	
	private float top;
	
	private float near = 1;
	
	private float far = 24;
	
	public Projection set2DScreen(Screen screen) {
		float width = screen.getWidth(); 
		float height = screen.getHeight();
		//重置投影矩阵
		float h_width = width / 2;
		float h_height = height / 2;
		left = -h_width;
		right = h_width; 
		bottom = -h_height;
		top = h_height; 
		near = 1f; 
		far = 24f;
		return this;
	}
	
	public Projection set3DScreen(Screen screen) {
		float width = screen.getWidth(); 
		float height = screen.getHeight();
		float ratio;
		//3D透视屏
		if(width > height) {
			ratio = width / height;
			left = -ratio;
			right = ratio;
			bottom = -1;
			top = 1;
			
		} else {
			ratio = height / width;
			left = -1;
			right = 1; 
			bottom = -ratio;
			top = ratio; 
		}
		near = 1f;
		far = 1024f;
		return this;
	}

	public void set(float left, float right, float bottom, float top) {
		this.left = left;
		this.right = right;
		this.bottom = bottom;
		this.top = top;
	}
	
	public void set(float left, float right, float bottom, float top, float near, float far) {
		this.left = left;
		this.right = right;
		this.bottom = bottom;
		this.top = top;
		this.near = near;
		this.far = far;
	}
	
	public void setLeft(float left) {
		this.left = left;
	}

	public void setRight(float right) {
		this.right = right;
	}

	public void setBottom(float bottom) {
		this.bottom = bottom;
	}

	public void setTop(float top) {
		this.top = top;
	}

	public void setNear(float near) {
		this.near = near;
	}

	public void setFar(float far) {
		this.far = far;
	}

	public float getLeft() {
		return left;
	}

	public float getRight() {
		return right;
	}

	public float getBottom() {
		return bottom;
	}

	public float getTop() {
		return top;
	}

	public float getNear() {
		return near;
	}

	public float getFar() {
		return far;
	}
	
	public void glProjection2D() {
		GLUtils.ortho(left, right, bottom, top, near, far);
	}
	
	public void glProjection2D(Matrix4f matrix) {
		matrix.ortho(left, right, bottom, top, near, far);
	}
	
	public void glProjection3D() {
		GLUtils.frustum(left, right, bottom, top, near, far);
	}	
	
	public void glProjection3D(Matrix4f matrix) {
		matrix.frustum(left, right, bottom, top, near, far);
	}	
}
